package org.jsrml.common.util;

import java.util.HashMap;
import java.util.Map;

/**
 * 工作单元
 * @author 张剑
 *
 */
public class UnitOfWork {

	/**
	 * 应用里的总工作单元池
	 */
	private static Map<String, UnitOfWork> unitOfWorkPool = new HashMap<String, UnitOfWork>();


	/**
	 * 记录器id
	 */
	private String id;

	/**
	 * 流程id
	 */
	private String sagaId;

	/**
	 * 根据命令id获取一个事件日志记录器实例
	 * 
	 * @param commandId
	 * @return
	 */
	public static UnitOfWork getInstance(String id, String sagaId) {

		UnitOfWork unitOfWork = unitOfWorkPool.get(id);

		if (unitOfWork == null) {
			unitOfWork = new UnitOfWork();
			unitOfWork.id = id;
			unitOfWork.sagaId = sagaId;
			unitOfWorkPool.put(id, unitOfWork);
		}

		return unitOfWork;
	}

	public void publish(boolean clear) {
		//	释放工作单元
		if (clear) {
			unitOfWorkPool.remove(id);
		}
	}


}
